package com.app.hmst.main;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.hmst.model.HomestayInfoVO;
import com.app.hmst.model.HouseInfoVO;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class SubMenuLevel2Adapter extends BaseAdapter{
	private List<HouseInfoVO> temp;
	private Activity act;
	private ViewHolderForFirst viewForFirst;
	private SlidingMenu _sm;
	private ListView _lvPrevious;
	private HomestayInfoVO homestayInfoVO;
	public SubMenuLevel2Adapter(Activity _act, List<HouseInfoVO> _temp,SlidingMenu sm,ListView lvPrevious,HomestayInfoVO home_info){
		this.act = _act;
		this.temp = _temp;
		this._sm = sm;
		this._lvPrevious = lvPrevious;
		this.homestayInfoVO = home_info;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return temp.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return temp.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			viewForFirst = new ViewHolderForFirst();
			LayoutInflater inflater = (LayoutInflater) act.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			convertView = inflater.inflate(R.layout.each_sub_level_two,null);
			viewForFirst.title = (TextView) convertView.findViewById(R.id.textView1);
			viewForFirst.subListMenuLevelTwo = (ListView) convertView.findViewById(R.id.subListMenuLevelTwo);
			viewForFirst.icon = (ImageView) convertView.findViewById(R.id.imageView1);;
			viewForFirst.each_level_two_menu_list = (RelativeLayout) convertView.findViewById(R.id.each_level_two_menu_list);
			
			
			final HouseInfoVO houseInfoVO = temp.get(position);
			
			viewForFirst.title.setText(houseInfoVO.name);
			
			viewForFirst.each_level_two_menu_list.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					switchFragment(new GeneralInfoForNewHomeFragment(_sm,homestayInfoVO,houseInfoVO));
				}
			});
			if("C".equals(houseInfoVO.status)){
				viewForFirst.icon.setImageDrawable(act.getResources().getDrawable(R.drawable.home_icon_selected));
			}else{
				viewForFirst.icon.setImageDrawable(act.getResources().getDrawable(R.drawable.home_icon));
			}
		
			convertView.setTag(viewForFirst);
		}
		return convertView;
	}
	public class ViewHolderForFirst {
		private TextView title;
		private ImageView icon;
		private ListView subListMenuLevelTwo;
		private RelativeLayout each_level_two_menu_list;
		
	}
	
	private void switchFragment(Fragment fragment) {
		if (act == null)
			return;

		if (act instanceof HomeStayMainActivity) {
			HomeStayMainActivity fca = (HomeStayMainActivity) act;
			fca.switchContent(fragment);
		}
	}
}
